#include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <unordered_map>
#include<algorithm>
#define N 10
// #define ONLINE_GUDGE
using namespace std;
const int dx[] = {-1,0,1,0},dy[] = {0,1,0,-1};
const string op = "urdl";
using PIS = pair<int, string>;
#define step first
#define str second 
string start;

int f(string state)//估计函数 计算曼哈顿距离 估计到终点的距离，并非实际距离，因此bfs的队列中的点不一定是最优解，最后输出的终点一定是最优解
{
    int dt=0;
    for(int i=0;i<9;i++)//这里1~8对应的下标为0~7
    if(state[i]!='x')
    {
        int t=state[i]-'1';//对应下标
        dt=dt+abs(i/3-t/3)+abs(i%3-t%3);//曼哈顿距离
    }
    return dt;//返回总曼哈顿距离
}


string Astar()
{
    string end = "12345678x";
    
    unordered_map <string,int> dist;
    unordered_map <string,pair <char,string>> prev;
    priority_queue <PIS,vector <PIS>,greater <PIS>> heap;
    dist[start] = 0;
    heap.push ({f (start),start});

    while(heap.size())
    {
        auto t = heap.top(); heap.pop();

        string state = t.str;
        if(state == end) break;

        int x, y;
        for(int i = 0; i < 9; i++) // 查找'x'的横纵坐标
            if(state[i] == 'x')
                {x = i / 3; y = i % 3; break;}

        string init = state; //因为要扩展到四个方向,所以要还原
        for(int k =0; k<4; k++)
        {
            int nx = x+dx[k], ny = y+dy[k];
            if(nx<0||nx>=3||ny<0||ny>=3) continue;//越界就跳过
            state = init; // 回溯
            swap(state[nx*3+ny], state[x*3+y]);//交换下标位置

            if(dist.count(state) == 0 || dist[state] > dist[init]+1)//如果没有被记录或者小于记录值
            {
                dist[state] = dist[init]+1;//更新距离
                heap.push({f(state)+dist[state] , state});//加入堆中
                prev[state]={op[k], init};//标记由哪种状态转移而来,并且记录执行的操作
            }

        }

    }

    // 倒退方案
    string ans;
    cout << "start: " << start << endl;
    while(end!=start) // end -> start
    {
        cout << "end:" << end << " ans: " << ans << endl;
        ans += prev[end].first;
        end = prev[end].second;
    }
    cout << "end:" << end << " ans: " << ans << endl;

    reverse(ans.begin(),ans.end());//将其反转
    return ans;

}
int main()
{
    #ifdef ONLINE_JUDGE

    #else
    freopen("./in.txt","r",stdin);
    #endif
    ios::sync_with_stdio(false);   
	cin.tie(0);

    int cnt = 0;
    string s; // 判是否有解

    for(int i = 0; i <= 8; i++)
    {
        char ch; cin >> ch;
        start += ch;
        if(ch != 'x') s += ch;
    }

    for (int i = 0;i < 8;i++) {
        for (int j = i + 1;j < 8;j++) {
            if (s[i] > s[j]) cnt++;
        }
    }
    cout << cnt << endl;
    cout << "s: " << s << endl;
    if(cnt % 2) printf("unsolvable\n"); // 如果逆序对个数是奇数 无解
    else cout << Astar() << endl; // 答案不唯一
    return 0;
}